package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrdersDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * C端订单相关接口
 */
@Api(value = "/user/order", tags = {"C端订单相关接口"})
@Slf4j
@RestController("UserOrderController")
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单提交功能
     * @param ordersSubmitDTO
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "OrdersSubmitDTO", name = "ordersSubmitDTO", value = "", required = true)
    })
    @ApiOperation(value = "订单提交功能", notes = "订单提交功能", httpMethod = "POST")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        return orderService.submit(ordersSubmitDTO);
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "OrdersPaymentDTO", name = "ordersPaymentDTO", value = "", required = true)
    })
    @ApiOperation(value = "订单支付", notes = "订单支付", httpMethod = "PUT")
    @PutMapping("/payment")
    public Result<Map<String,LocalDateTime>> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
//        log.info("订单支付：{}", ordersPaymentDTO);
//        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
//        log.info("生成预支付交易单：{}", orderPaymentVO);
        return orderService.payment(ordersPaymentDTO);
    }

    /**
     * 查询订单详情
     * @param orderId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "orderId", value = "", required = true)
    })
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情", httpMethod = "GET")
    @GetMapping("/orderDetail/{orderId}")
    public Result<OrdersDetailVO> orderDetail(@PathVariable("orderId") Long orderId) throws Exception {
        return orderService.getOrderDetail(orderId);
    }

    /**
     * 催单功能
     * @param orderId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "orderId", value = "", required = true)
    })
    @ApiOperation(value = "催单功能", notes = "催单功能", httpMethod = "GET")
    @GetMapping("/reminder/{orderId}")
    public Result reminder(@PathVariable("orderId") Long orderId) throws Exception {
        return orderService.reminder(orderId);
    }

    /**
     * 再来一单功能
     * @param orderId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "orderId", value = "", required = true)
    })
    @ApiOperation(value = "再来一单功能", notes = "再来一单功能", httpMethod = "POST")
    @PostMapping("repetition/{orderId}")
    public Result repetition(@PathVariable("orderId") Long orderId) throws Exception {
        //再来一单就是将原订单中的商品重新加入到购物车中
        return orderService.repetition(orderId);
    }

    /**
     * 分页查询订单
     * @param pageSize
     * @param page
     * @param status
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "分页查询订单", notes = "分页查询订单", httpMethod = "GET")
    @GetMapping("/historyOrders")
    public Result<PageResult> pageQueryHistoryOrders(Integer pageSize,Integer page,Integer status) throws Exception {
        return orderService.pageQueryOrders(pageSize,page,status);
    }

    /**
     * 取消订单功能
     * @param orderId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "orderId", value = "", required = true)
    })
    @ApiOperation(value = "取消订单功能", notes = "取消订单功能", httpMethod = "PUT")
    @PutMapping("/cancel/{orderId}")
    public Result cancel(@PathVariable("orderId") Long orderId) throws Exception {
        return orderService.UserCancel(orderId);
    }
}
